<?php
use Workerman\Worker;
use Workerman\Lib\Timer;
require_once __DIR__ .'/Workerman/Autoloader.php';
error_reporting(E_ALL & ~E_NOTICE);
ini_set('date.timezone','Asia/Shanghai');
//包含数据库操作类文件
include 'mysql.class.php';
include 'config.php';

$db = new Mysql($host,$username,$password,$dbname);

$bonussql=$db->getOne("select * from jz_options where option_name='bonus'");
$bonus=json_decode($bonussql['option_value'],true);
$extractsql=$db->getOne("select * from jz_options where option_name='extract'");
$extract=json_decode($extractsql['option_value'],true);
$Room=array();
$Room['id']=0;
$Room['xx']=array();
$Room['user']=array();
$worker = new Worker('websocket://0.0.0.0:'.$serverdk);
$worker->onWorkerStart = function($worker)
{

};
$worker->onConnect = function($connection)
{
    ouput("新的链接ip为 " .$connection->getRemoteIp());
};

$worker->onMessage = function($connection, $data)
{	
    global $db;
    global $bonus;
    global $extract;
    $dataInfo=json_decode($data,true);
    if($dataInfo['timeout']){
        if($dataInfo['timeout']-time()>0){
        $timer_id = Timer::add($dataInfo['timeout']-time(), function()use(&$timer_id, &$connection, &$data)
            {
                $connection->send($data);
                Timer::del($timer_id);
            });
        }else{
            $connection->send($data);
        }
        $dataxx=array();
        $dataxx['act']='djs';
        $dataxx['id']=$timer_id;
        $dataxx['room']=$dataInfo['room'];
        $connection->send(json_encode($dataxx));
    }
    elseif($dataInfo['overtime']==1){
            Timer::del($dataInfo['id']);
    }else{
		switch($dataInfo['act']){
			case 'open':
				$server=$db->getOne("select * from jz_server where id=".$dataInfo['id']);
				if($server['zt']==1){
					return act('error',"服务器已经启动",$connection);
				}
				$id=$server['id'];
				if($server['type']!='0'){
					shell_exec('php game'.$server['type'].'.php start -d '.$id.' '.$server['dk']);
				} else {
					shell_exec('php test.php start -d '.$id.' '.$server['dk']);
				}
				act('success',"等待服务器打开",$connection);
			break;
			case 'close':
				$server=$db->getOne("select * from jz_server where id=".$dataInfo['id']);
				if($server['zt']==0){
					return act('error',"服务器未启动",$connection);
				}
				if($server['type']!='0'){
					shell_exec('php game'.$server['type'].'.php stop');
				} else {
					shell_exec('php test.php stop');
				}
				act('success',"等待关闭",$connection);
			break;
			default:reqact($dataInfo,$connection); 
		}
        
    }  
};

$worker->onClose = function($connection)
{
    global $db;
    if($connection->task){
        $server=$db->getOne("select * from jz_server where id='".$connection->task."'");
        $map['zt']=0;
        $map['num']=0;
        ouput($server['title'].'-'.$server['id'].'断开到服务器的链接');
        $db->update('jz_server',$map,"id=".$connection->task);
        httz($server['title'].'-'.$server['id']."断开到服务器的链接");
    }
    if($connection->cjq){
        $cjq=$db->getOne("select * from jz_cjq where id='".$connection->cjq."'");
        $map=array();
        $map['token']=0;
        ouput($cjq['name'].'断开到服务器的链接');
        $db->update('jz_cjq',$map,"id=".$connection->cjq);
    }
};
// 运行worker
Worker::runAll();
function tzsql($connection){
    global $host;
    global $username;
    global $password;
    global $dbname;
    global $charset;
    global $url;
    $hostxx['hostname']=$host;
    $hostxx['username']=$username;
    $hostxx['password']=$password;
    $hostxx['dbname']=$dbname;
    $hostxx['charset']=$charset;
    $data['act']='start';
    $data['host']=$hostxx;
    $data['url']=$url;
    $connection->send(json_encode($data));
}
function httz($msg){
    global $worker;
    foreach($worker->connections as $connection)
        {   
                    if($connection->sfxt){
                        act('success',$msg,$connection);
                    }
        }
}
function reqact($data2,$connection){
    global $db;
    global $bonus;
    global $extract;
    if($connection->sfxt==1){
        $tpl=file_get_contents("sys/sact/".$data2['act'].".php");
    }
    elseif($connection->task){
        $tpl=file_get_contents("sys/task/".$data2['act'].".php");
    }
    else{
        $tpl=file_get_contents("sys/act/".$data2['act'].".php");
    }
    $tpl=str_replace("<?php","",$tpl);
    $tpl=str_replace("?>","",$tpl);
    eval($tpl);
}
function ouput($str){
		echo $str."\n";
}
function error($msg,$connection){
    $data['msg']=$msg;
    $data['act']='error';
    $connection->send(json_encode($data));
    return false;
}
function act($act,$sj,$connection){
    $data['msg']=$sj;
    $data['act']=$act;
    $connection->send(json_encode($data));
    return false;
}
?>